/*
 * Created by IntelliJ IDEA.
 * User: rousseau
 * Date: Apr 21, 2002
 * Time: 3:16:48 PM
 * To change template for new class use
 * Code Style | Class Templates options (Tools | IDE Options).
 */
package org.jfract.util;

import org.jfract.abstractBeans.AbstractFractalColorBean;

import android.graphics.Bitmap;

/**
 * This class is a thread. The goal is to compute the graphics with a thread that give a time for the processor
 */
public class PictureColorProcessor
    extends Thread
{

  private AbstractFractalColorBean fractalBean;

  private CompletionListener listener;

  private Bitmap img;

  private boolean repaint;

  private static String[] rmiList;

  private Object threadLock = new Object();

  private boolean running = true;

  private boolean pause = false;

  public void setRunning(boolean running)
  {
    this.running = running;
  }

  /**
   * notify the thread
   */
  public void notifyThread()
  {
    this.pause = false;
    synchronized (threadLock)
    {
      threadLock.notify();
      // log4j_category.info("Resuming process");
    }
  }

  /**
   * pause the thread wait on lock object
   */
  public void pause()
  {
    // log4j_category.info("Pausing process");
    pause = true;
  }

  public void stopThread()
  {
    interrupt();
  }

  /**
   * the constructor
   * 
   * @param fractalBean
   *          the fractal bean
   * @param img
   *          the bufferedImage to paint on
   * @param listener
   *          the completion listener to tell the completion
   * @param repaint
   *          reapint the componant at the end
   */
  public PictureColorProcessor(AbstractFractalColorBean fractalBean,
      Bitmap img, CompletionListener listener, boolean repaint)
  {
    this.fractalBean = fractalBean;
    this.listener = listener;
    this.setPriority(Thread.MAX_PRIORITY);
    this.img = img;
    this.repaint = repaint;
  }

  /**
   * If this thread was constructed using a separate <code>Runnable</code> run object, then that <code>Runnable</code> object's <code>run</code>
   * method is called; otherwise, this method does nothing and returns.
   * <p>
   * Subclasses of <code>Thread</code> should override this method.
   * 
   * @see Thread#start()
   * @see Thread#stop()
   * @see Thread#Thread(ThreadGroup, Runnable, String)
   * @see Runnable#run()
   */
  public void run()
  {

    // fractalBean.getJBeanable().getJBean().getPanelBean().setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR));
    int i, j;

    // Dimension dim = fractalBean.getJBeanable().getJBean().getPanelBean().getSize ();
    Dimension dim = new Dimension(img.getWidth(), img.getHeight());
    // System.out.println("dim "+ dim);
    fractalBean.computeDelta(dim.getWidth(), dim.getHeight());
    int total = dim.getWidth() * dim.getHeight();
    long timeStart = System.currentTimeMillis();
    fractalBean.computeDelta(dim.getWidth(), dim.getHeight());

    int max = dim.getHeight() * dim.getWidth();
    int deltaI = new Double(max / 100).intValue();
    int processFlag = deltaI;

    for (j = 0; j < dim.getHeight(); j++)
    {
      for (i = 0; i < dim.getWidth(); i++)
      {
        if (running)
        {
          int color = fractalBean.computeFractal(fractalBean.getXmin() + i * fractalBean.getDeltax(), fractalBean.getYmin() + j * fractalBean.getDeltay());
          img.setPixel(i, j, color);
          if ((j * dim.getWidth() + i) % deltaI == 0 && (j * dim.getWidth() + i) % deltaI != max)
          {
            // processFlag += deltaI;
            if (listener != null)
              listener.completionEventPerformed(j * dim.getWidth() + i, max);
            try
            {
              sleep(1);
              // if pause just wait and resume by calling notifyThread()
              if (pause)
              {
                synchronized (threadLock)
                {
                  threadLock.wait();
                }
              }
            }
            catch (InterruptedException e)
            {
              // log4j_category.info("interrupted Thread : " + getName());
              break;
            }
          }
        }
        else
          break;
      }
    }

    if (listener != null)
      listener.completionEventPerformed(max, max);
    // if (repaint)
    // {
    // fractalBean.getJBeanable().getJBean().getPanelBean().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    // fractalBean.getJBeanable().getJBean().getPanelBean().repaint ();
    // }

  }
}
